package com.kye.express.business.common_tool;

import android.content.Context;
import android.util.Pair;

import com.kye.base.Logger;
import com.kye.express.App;
import com.kye.express.R;
import com.kye.model.AppDatabase;
import com.kye.model.bean.common_tool.CommonToolItemBean;
import com.kye.model.dao.CommonToolDao;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.FlowableEmitter;
import io.reactivex.FlowableOnSubscribe;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.Single;
import io.reactivex.SingleEmitter;
import io.reactivex.SingleOnSubscribe;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Function;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 07/01/2018
 * @updated : 07/01/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public class CommonToolItemUtils {

    /**
     * 初始化数据并返回数据
     * @return
     */
    public static Observable<List<CommonToolItemBean>> initCommonToolData() {
        final CommonToolDao commonToolDao = AppDatabase.getInstance(App.getContext().getApplicationContext())
                .commonToolDao();
        // 获取当前db数据
        Single<List<CommonToolItemBean>> queryAll = getInitCommonDataDB();
        // 获取初始化数据
        Single<List<CommonToolItemBean>> queryAllInit = getInitCommonData();
        return
                Observable.zip(queryAll.toObservable(), queryAllInit.toObservable(), new BiFunction<List<CommonToolItemBean>, List<CommonToolItemBean>, List<CommonToolItemBean>>() {
                    @Override
                    public List<CommonToolItemBean> apply(List<CommonToolItemBean> commonToolItemDb, List<CommonToolItemBean> commonToolItemInit) throws Exception {
                        if (commonToolItemDb == null || commonToolItemDb.size() <= 0) {
//                            Logger.d("use init data");
                            commonToolDao.insert(commonToolItemInit);
                            return commonToolItemInit;
                        } else {
//                            Logger.d("use db data");
                            return commonToolItemDb;
                        }
                    }
                });
    }

    /**
     * 获取当前数据库保存的常用工具数据
     * @return
     */
    public static Single<List<CommonToolItemBean>> getInitCommonDataDB() {
        return
                Single.create(new SingleOnSubscribe<List<CommonToolItemBean>>() {
                    @Override
                    public void subscribe(SingleEmitter<List<CommonToolItemBean>> e) throws Exception {
                        try {
                            List<CommonToolItemBean> commonToolItemBeans = AppDatabase.getInstance(App.getContext())
                                    .commonToolDao().queryAll();
                            if (commonToolItemBeans == null) {
                                commonToolItemBeans = new ArrayList<>();
                            }
                            e.onSuccess(commonToolItemBeans);
                        } catch (Throwable throwable) {
                            e.onError(throwable);
                        }

                    }
                });
    }

    /**
     * 获取默认的常用工具初始化数据
     * @return
     */
    public static Single<List<CommonToolItemBean>> getInitCommonData() {
        return
                Flowable
                        .create(new FlowableOnSubscribe<Pair<List<String>, List<Integer>>>() {
                            @Override
                            public void subscribe(FlowableEmitter<Pair<List<String>, List<Integer>>> e) throws Exception {
                                List<String> commonToolNames = Arrays.asList(App.getInstance().getResources().getStringArray(R.array.common_tool_items_names));
                                int[] commonToolIdsArray = App.getInstance().getResources().getIntArray(R.array.common_tool_items_ids);
                                List<Integer> commonToolIds = new ArrayList<>();
                                for (int id : commonToolIdsArray) {
                                    commonToolIds.add(id);
                                }
                                e.onNext(new Pair<List<String>, List<Integer>>(commonToolNames, commonToolIds));
                                e.onComplete();
                            }
                        }, BackpressureStrategy.DROP)
                        .flatMap(new Function<Pair<List<String>, List<Integer>>, Flowable<CommonToolItemBean>>() {
                            @Override
                            public Flowable<CommonToolItemBean> apply(Pair<List<String>, List<Integer>> listListPair) throws Exception {
                                Flowable<String> nameFlowable = Flowable.fromIterable(listListPair.first);
                                Flowable<Integer> idsFlowable = Flowable.fromIterable(listListPair.second);
                                Flowable<CommonToolItemBean> zip = Flowable.zip(nameFlowable, idsFlowable, new BiFunction<String, Integer, CommonToolItemBean>() {
                                    @Override
                                    public CommonToolItemBean apply(String name, Integer id) throws Exception {
//                                        Logger.d("name : " + name + " , id : " + id);
                                        // 默认isSelected : false 位置使用positionunselected
                                        return new CommonToolItemBean(id, name, false, id - 1, 0, id - 1);
                                    }
                                });
                                return zip;
                            }
                        })
                        .toList();
    }

    public static Observable<List<CommonToolItemBean>> getAllCommonToolList(final Context context) {
        return
                Observable
                        .create(new ObservableOnSubscribe<Pair<List<String>, List<Integer>>>() {
                            @Override
                            public void subscribe(ObservableEmitter<Pair<List<String>, List<Integer>>> e) throws Exception {
                                List<String> commonToolNames = Arrays.asList(context.getResources().getStringArray(R.array.common_tool_items_names));
                                int[] commonToolIdsArray = context.getResources().getIntArray(R.array.common_tool_items_ids);
                                List<Integer> commonToolIds = new ArrayList<>();
                                for (int id : commonToolIdsArray) {
                                    commonToolIds.add(id);
                                }
                                Logger.d("commonToolNames : " + commonToolNames + " , commonToolIds : " + commonToolIds);
                                e.onNext(new Pair<List<String>, List<Integer>>(commonToolNames, commonToolIds));
                                e.onComplete();
                            }
                        })
                        .flatMap(new Function<Pair<List<String>, List<Integer>>, ObservableSource<CommonToolItemBean>>() {
                            @Override
                            public ObservableSource<CommonToolItemBean> apply(Pair<List<String>, List<Integer>> listListPair) throws Exception {
                                Observable<String> nameObservable = Observable.fromIterable(listListPair.first);
                                Observable<Integer> idsObservable = Observable.fromIterable(listListPair.second);
                                Observable<CommonToolItemBean> zip = Observable.zip(nameObservable, idsObservable, new BiFunction<String, Integer, CommonToolItemBean>() {
                                    @Override
                                    public CommonToolItemBean apply(String name, Integer id) throws Exception {
                                        Logger.d("name : " + name + " , id : " + id);
                                        return new CommonToolItemBean(id, name, false, id - 1, 0, 0);
                                    }
                                });
                                return zip;
                            }
                        })
                        .toList()
                        .toObservable();
    }

//    public static Observable<List<CommonToolItemModel>> getAllCommonToolListBak(final Context context) {
//        return
//                Observable
//                        .create(new ObservableOnSubscribe<Pair<List<String>, List<Integer>>>() {
//                            @Override
//                            public void subscribe(ObservableEmitter<Pair<List<String>, List<Integer>>> e) throws Exception {
//                                List<String> commonToolNames = Arrays.asList(context.getResources().getStringArray(R.array.common_tool_items_names));
//                                int[] commonToolIdsArray = context.getResources().getIntArray(R.array.common_tool_items_ids);
//                                List<Integer> commonToolIds = new ArrayList<>();
//                                for (int id : commonToolIdsArray) {
//                                    commonToolIds.add(id);
//                                }
//                                Logger.d("commonToolNames : " + commonToolNames + " , commonToolIds : " + commonToolIds);
//                                e.onNext(new Pair<List<String>, List<Integer>>(commonToolNames, commonToolIds));
//                                e.onComplete();
//                            }
//                        })
//                        .flatMap(new Function<Pair<List<String>, List<Integer>>, ObservableSource<CommonToolItemModel>>() {
//                            @Override
//                            public ObservableSource<CommonToolItemModel> apply(Pair<List<String>, List<Integer>> listListPair) throws Exception {
//                                Observable<String> nameObservable = Observable.fromIterable(listListPair.first);
//                                Observable<Integer> idsObservable = Observable.fromIterable(listListPair.second);
//                                Observable<CommonToolItemModel> zip = Observable.zip(nameObservable, idsObservable, new BiFunction<String, Integer, CommonToolItemModel>() {
//                                    @Override
//                                    public CommonToolItemModel apply(String name, Integer id) throws Exception {
//                                        Logger.d("name : " + name + " , id : " + id);
//                                        return new CommonToolItemModel(id, name, id - 1, false, 0);
//                                    }
//                                });
//                                return zip;
//                            }
//                        })
//                        .map(new Function<CommonToolItemModel, CommonToolItemModel>() {
//                            @Override
//                            public CommonToolItemModel apply(CommonToolItemModel commonToolItemModel) throws Exception {
//                                switch (commonToolItemModel.getId()) {
//                                    case 1:
//                                        commonToolItemModel.setIconRes(R.drawable.ic_icon_product_33);
//                                        break;
//                                    case 2:
//                                        commonToolItemModel.setIconRes(R.drawable.icon_new_home_customer);
//                                        break;
//                                    case 3:
//                                        commonToolItemModel.setIconRes(R.drawable.icon_new_home_freight);
//                                        break;
//                                    case 4:
//                                        commonToolItemModel.setIconRes(R.drawable.icon_new_home_time_query);
//                                        break;
//                                    case 5:
//                                        commonToolItemModel.setIconRes(R.drawable.tool_freight_payment);
//                                        break;
//                                    case 6:
//                                        commonToolItemModel.setIconRes(R.drawable.icon_new_home_prestore);
//                                        break;
//                                    case 7:
//                                        commonToolItemModel.setIconRes(R.drawable.ic_online_tick);
//                                        break;
//                                    case 8:
//                                        commonToolItemModel.setIconRes(R.drawable.ic_site_query);
//                                        break;
//                                    case 9:
//                                        commonToolItemModel.setIconRes(R.drawable.order_take_camera);
//                                        break;
//                                    case 10:
//                                        commonToolItemModel.setIconRes(R.drawable.flight_prohibit);
//                                        break;
//                                    case 11:
//                                        commonToolItemModel.setIconRes(R.drawable.jd_pre_order);
//                                        break;
//                                    case 12:
//                                        commonToolItemModel.setIconRes(R.drawable.fan_support);
//                                        break;
//                                    case 13:
//                                        commonToolItemModel.setIconRes(R.drawable.lucky_money_get);
//                                        break;
//                                    default:
//                                        break;
//                                }
//                                return commonToolItemModel;
//                            }
//                        })
//                        .toList()
//                        .toObservable();
//    }
}
